package com.example.cloudAlibaba.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.example.cloudAlibaba.myHandler.CustomerBlockHandler;
import com.example.springcloud.utils.Constant;
import com.example.springcloud.utils.ResultEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/service")
public class RateLimitController {

    @GetMapping("/byResource")
    @SentinelResource(value = "byResource", blockHandler = "handleException")
    public ResultEntity byResource() {

        return new ResultEntity<>(Constant.RESPONSE_CODE_200, "按资源名称限流测试ok");
    }

    public ResultEntity handleException(BlockException exception) {

        return new ResultEntity(Constant.RESPONSE_CODE_500, "服务不可用 /(ㄒoㄒ)/~~");
    }

    @GetMapping("/byCustomerBlock")
    @SentinelResource(value = "byCustomerBlock", blockHandlerClass = CustomerBlockHandler.class, blockHandler = "handlerException2")
    public ResultEntity byCustomerBlock() {

        return new ResultEntity<>(Constant.RESPONSE_CODE_200, "按客户自定义限流测试ok");
    }

}
